package ModelLayer;

import java.util.ArrayList;
import java.util.Date;

public class Salg {
	private boolean betalt;
	private Date salgsDato;
	private int salgsNr;
	private float pris;
	private ArrayList<DelSalg> salgsListe;
	//private int produktGruppe;
	private Kunde kunde;
	
	/**
	 * For adding kunde to the sale*/
	private static KundeContainer kCon;
	/**
	 * Create Salgs Object and set tlf
	 * @param String tlf
	 * */
	public Salg()
	{
		kCon = KundeContainer.getInstance();
		salgsDato = new Date();
		salgsListe = new ArrayList<DelSalg>();
	}
	
	/**
	 * Set if paid status.
	 * @param boolean paid
	 * */
	public void setBetalt(boolean t)
	{
		this.betalt = t;
	}
	/**
	 * Get paid status
	 * @return boolean betalt
	 * */
	public boolean getBetalt()
	{
		return this.betalt;
	}
	/**
	 * Set price cut for the sale
	 * @param int percent
	 * The Math in the method:
	 * convert percent amount to a 0,xx number.
	 * take 1.0 - 0,xx to get what to multiply the 
	 * price with.
	 * result: price with the price cut given.
	 * */
	public void setExtraRabat(int p)
	{
		float per = p/100;
		float perc = 1-per;
		pris = perc * pris;
	}
	/**
	 * Add kunde to the Salg 
	 * @param String tlf*/
	public void addKunde(String tlf)
	{
		kunde = kCon.findKunde(tlf);
		addKundeRabat();
	}
	/**
	 * remove Kunde from the sale
	 * set kunde field to null*/
	public void removeKunde()
	{
		kunde = null;
	}
	/**
	 * Get kunde thats connected to this sale
	 * @return Kunde kunde*/
	public Kunde getKunde()
	{
		return kunde;
	}
	/**
	 * Add DelSalg object to the salgsListe
	 * @param int artikelNr, int antal*/
	public void addDelSalg(int aN, int a)
	{
		DelSalg d = new DelSalg(a, aN);
		salgsListe.add(d);
	}
	/**
	 * Remove a delSalg from the delsalgContainer.
	 * @param int artikelNr*/
	public void removeDelSalg(int aN)
	{
		boolean search = false;
		int index = 0;
		DelSalg ds = null;
		while(!search && index < salgsListe.size()){
			ds = salgsListe.get(index);
			if(ds.getVareAN(aN) != null){
				search = true;
			}else{
				index++;
			}
		}
		if(search){
			salgsListe.remove(index);
		}
		
	}
	/**
	 * Get the total price of all DelSalgs together.
	 * @return float price
	 * */
	public float getTotalPrice()
	{
		float p = 0;
		for(DelSalg x: salgsListe){
			p += x.getTotalPrice();
		}
		return p;
	}
	/**
	 *Get and set salgsDato
	 *@param String salgsDato
	 *@return String salgsDato */
	public void setSalgsDato()
	{
		this.salgsDato = new Date();
	}
	public Date getSalgsDato()
	{
		return this.salgsDato;
	}
	/**
	 * Set and get salgsNr
	 * @param int salgsNr
	 * @return int salgsNr*/
	public void setSalgsNr(int sN)
	{
		salgsNr = sN;
	}
	public int getSalgsNr()
	{
		return salgsNr;
	}
	/**
	 * Add the Kunde rabat as rabat to the salg
	 * 
	 * */
	public void addKundeRabat()
	{
		setExtraRabat(kunde.getKundeRabat());
	}
	/**
	 * Get amount of delSalg in a Salg
	 * @return ArrayList<DelSalg>
	 * */
	public ArrayList<DelSalg> getAllDelSalg()
	{
		return salgsListe;
	}
}
